package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Teacher;
import com.czz.manage.query.TeacherQuery;
import com.czz.manage.service.TeacherService;
import com.czz.manage.vo.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName: TeacherAdminController
 * @Author Czz
 * @Date 2020/1/6
 * @Time 15:09
 **/
@RestController
@RequestMapping(value = "/admin/teachers")
public class TeacherAdminController {

    @Autowired
    TeacherService teacherService;

    /**
    * @Description 分页查询所有讲师
    * @Param [page, limit, teacherQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/7
    * @Time 15:44
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getTeacherPageList(@PathVariable Long page, @PathVariable Long limit, TeacherQuery teacherQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.getTeacherPageList(pageParam,teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 根据ID删除讲师
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/7
    * @Time 15:44
    **/
    @DeleteMapping(value = "/{id}")
    public R deleteTeacherById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Boolean flag = teacherService.deleteTeacherById(id);
        if (flag) return R.ok();
        else return R.setResult(ResultCodeEnum.INPUT_ERROR).message("删除失败！该讲师还存在课程！");
    }

    /**
    * @Description 根据ID禁用/启用讲师
    * @Param [id, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/7
    * @Time 15:44
    **/
    @PutMapping(value = "/{id}/{status}")
    public R updateTeacherStatusById(@PathVariable String id,@PathVariable Integer status){
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        teacherService.updateTeacherStatusById(id, status);
        return R.ok();
    }

    /**
    * @Description 根据id修改讲师
    * @Param [id, teacher]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/7
    * @Time 15:45
    **/
    @PutMapping(value = "{id}")
    public R updateTeacherById(@PathVariable String id,Teacher teacher){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(teacher)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        teacher.setTeacherId(id);
        teacherService.updateTeacherById(teacher);
        return R.ok();
    }

    /**
    * @Description 根据ID查询讲师
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/7
    * @Time 17:01
    **/
    @GetMapping(value = "{id}")
    public R getTeacherById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    /**
    * @Description 根据讲师id获取讲师详细信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 11:18
    **/
    @GetMapping(value = "/detail/{id}")
    public R getTeacherUserById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        TeacherVo teacherVo = teacherService.getTeacherUserById(id);
        return R.ok().data("item",teacherVo);
    }
}
